<template>
  <!-- table区域-begin -->
  <div>
    <a-table
      ref="table"
      size="middle"
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :expandedRowKeys="expandedRowKeys"
      @change="handleTableChange"
      @expand="handleExpand"
      v-bind="tableProps">
    </a-table>
  </div>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { YcListMixin } from '@/mixins/YcListMixin'
  import { filterMultiDictText } from '@/components/dict/YchDictSelectUtil'
  import { EFormConstant } from '../../EFormConstant'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'ActAppGroupList',
    mixins: [YcListMixin],
    data () {
      return {
        columns: [
          {
            title: this.$t('m.eform.appgroup.entity.groupName'),
            align: 'left',
            dataIndex: 'groupName'
          },
          {
            title: this.$t('m.eform.appgroup.entity.groupCode'),
            align: 'left',
            dataIndex: 'groupCode'
          },
          {
            title: this.$t('m.eform.formDesign.label.lei-xing'),
            align: 'left',
            dataIndex: 'groupType',
            customRender: (text) => {
              if (!text) {
                return ''
              } else {
                return filterMultiDictText(EFormConstant.groupType, text + '')
              }
            }
          },
          {
            title: this.$t('m.eform.appview.verify.parameterName'),
            align: 'left',
            dataIndex: 'orderNum'
          }
        ],
        url: {
          list: YchConstant.PREFIX_EFORM + '/manage/actAppGroup/rootList',
          childList: YchConstant.PREFIX_EFORM + '/manage/actAppGroup/childList',
        },
        expandedRowKeys: [],
        hasChildrenField: 'hasChild',
        pidField: 'parentId',
        /* 排序参数 */
        isorter: {
          column: 'orderNum',
          order: 'asc',
        },
      }
    },
    computed: {
      tableProps () {
        let _this = this
        return {
          // 列表项是否可选择
          rowSelection: {
            selectedRowKeys: _this.selectedRowKeys,
            onChange: (selectedRowKeys) => _this.selectedRowKeys = selectedRowKeys
          }
        }
      }
    },
    methods: {
      loadData (arg) {
        if (arg == 1) {
          this.ipagination.current = 1
        }
        this.loading = true
        this.expandedRowKeys = []
        let params = this.getQueryParams()
        return new Promise((resolve) => {
          getAction(this.url.list, params).then(res => {
            if (res.success) {
              let result = res.result
              if (Number(result.total) > 0) {
                this.ipagination.total = Number(result.total)
                this.dataSource = this.getDataByResult(res.result.records)
                resolve()
              } else {
                this.ipagination.total = 0
                this.dataSource = []
              }
            } else {
              this.messageUtil.warning(res.message)
            }
            this.loading = false
          })
        })
      },
      getDataByResult (result) {
        return result.map(item => {
          //判断是否标记了带有子节点
          if (item[this.hasChildrenField] == '1') {
            let loadChild = { id: item.id + '_loadChild', name: 'loading...', isLoading: true }
            item.children = [loadChild]
          }
          return item
        })
      },
      handleExpand (expanded, record) {
        // 判断是否是展开状态
        if (expanded) {
          this.expandedRowKeys.push(record.id)
          if (record.children.length > 0 && record.children[0].isLoading === true) {
            let params = this.getQueryParams()//查询条件
            params[this.pidField] = record.id
            getAction(this.url.childList, params).then((res) => {
              if (res.success) {
                if (res.result && res.result.length > 0) {
                  record.children = this.getDataByResult(res.result)
                  this.dataSource = [...this.dataSource]
                } else {
                  record.children = ''
                  record.hasChildrenField = '0'
                }
              } else {
                this.messageUtil.warning(res.message)
              }
            })
          }
        } else {
          let keyIndex = this.expandedRowKeys.indexOf(record.id)
          if (keyIndex >= 0) {
            this.expandedRowKeys.splice(keyIndex, 1)
          }
        }
      },
      expandTreeNode (nodeId) {
        return new Promise((resolve, reject) => {
          this.getFormDataById(nodeId, this.dataSource)
          let row = this.parentFormData
          this.expandedRowKeys.push(nodeId)
          let params = this.getQueryParams()//查询条件
          params[this.pidField] = nodeId
          getAction(this.url.childList, params).then((res) => {
            if (res.success) {
              if (res.result && res.result.length > 0) {
                row.children = this.getDataByResult(res.result)
                this.dataSource = [...this.dataSource]
                resolve()
              } else {
                reject()
              }
            } else {
              reject()
            }
          })
        })
      },
      getFormDataById (id, arr) {
        if (arr && arr.length > 0) {
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].id == id) {
              this.parentFormData = arr[i]
            } else {
              this.getFormDataById(id, arr[i].children)
            }
          }
        }
      }
    }
  }
</script>
